
<div style="width:100%;height:150px">
    <table id="addTartonTable"  style="width:100%;height:auto"></table>
    <div id='addCartonUnitPriceDialog' >
        <table id='addCartonUnitPriceTable'></table>
        <div id="addCartonUnitPriceTable_tb" style="padding:2px 5px;display: none">
            组织:
            <select id="cartonPkOrg" class="easyui-combobox" panelHeight="auto" style="width:150px">
                <option value="澳宝化妆品（惠州）有限公司">澳宝化妆品（惠州）有限公司</option>
                <option value="兆婷">惠州兆婷化妆品有限公司</option>
            </select>
            纸板型号:
            <input class="easyui-textbox" id="cartonCode" name="code">
            <a href="#" data-options="onClick:addCartonUnitPriceTableInit" class="easyui-linkbutton" iconCls="icon-search">查询</a>
        </div>
    </div>
    <div id="addTartonTable_tb2" style="text-align: right;display: none">
        <a href="#" style="margin-right: 50px"  class="easyui-linkbutton"  onclick="javaScript:$('#addTartonTable').edatagrid('loadData',[]);cutAddTartontableInit()" >切换外箱</a>
        <label style="margin-right: 30px">龙卡(啤)公式：【(长/2.54+0.5)*(宽/2.54+0.5)*单价/装箱数量*刀卡数量+0.05】</label>
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="getCartonUnitPrice(2)" ></a>
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="javaScript:$('#addTartonTable').edatagrid('destroyRow')" ></a>
    </div>

    <div id="addTartonTable_tb" style="text-align: right;display: none">
        <a href="#" style="margin-right: 50px" class="easyui-linkbutton"  onclick="javaScript:$('#addTartonTable').edatagrid('loadData',[]);cutAddTartonTableType2()" >切换龙卡(啤)</a>
        <label style="margin-right: 30px">纸箱公式：【(长+宽+5.08)*(宽+高+2.54)*2*单价/6451.6】</label>
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="getCartonUnitPrice(1)" ></a>
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="javaScript:$('#addTartonTable').edatagrid('destroyRow')" ></a>
    </div>
</div>
</div>
<script th:inline="none">
    //单击"+"获取价格
    function getCartonUnitPrice(a) {
        $("#addCartonUnitPriceDialog").dialog({
            title:'纸箱单价(98折)',
            modal:true,
            width:750,
            height:350,
            onOpen:function () {
                //第一次默认根据用户的组织获取纸板单价
                addCartonUnitPriceTableInit(getSessionUser().pkOrg,a); //a是类型 1、纸箱，2、刀卡
            },onClose:function () {
            }
        })
    }

    //保存单价和纸板名称方法  || 纸箱单价(98折)【查询】方法
    function addCartonUnitPriceTableInit(pkOrg,a) {

        if (pkOrg){
            if (pkOrg=="0001O110000000001HET"){
                pkOrg="澳宝化妆品（惠州）有限公司"
            }else {
                pkOrg="惠州兆婷化妆品有限公司"
            }
        }else {
            pkOrg=$("#cartonPkOrg").combobox("getValue");
        }
        var code=$("#cartonCode").textbox("getValue");
        $("#addCartonUnitPriceTable").datagrid({
            singleSelect:true,
            nowrap:false,
            toolbar:'#addCartonUnitPriceTable_tb',
            url:'/costReportCarton/getAllCartonDetails',
            queryParams:{
                pkOrg:pkOrg,
                code:code
            },
            columns:[[
                {title:'公司名称',field:'companyName',align:'center',width:200},
                {title:'纸板型号',field:'code',align:'center',width:80},
                {title:'材质',field:'cd1',align:'center',width:165},
                {title:'报价日期',field:'quotationDate',align:'center',width:120},
                {title:'含税单价',field:'payPrice',align:'center',width:60},
                {title:'含税单价(98折)',field:'payPrice98',align:'center',width:90},
            ]],
            onDblClickRow:function (index, row) {
                var str="纸箱";
                if(a==2){
                    str="龙卡(啤)"
                }
                $('#addTartonTable').edatagrid('addRow',{
                    row:{
                        name:str+'('+row.code+')',
                        price:row.payPrice98,
                        t:a,
                    }
                })
                $("#addCartonUnitPriceDialog").dialog('close');

            }
        })
    }

    //初始化计算纸箱的表格，打开窗口后默认的
    function cutAddTartonTableType(){
        //cartonTableData若有数据：根据类型显示表格，若没有数据直接显示纸箱
        if (cartonTableData.length>0){
            cutAddTartontableInit()
        }else if (cartonTableDataK.length>0) {
            cutAddTartonTableType2();

        }else {
            cutAddTartontableInit()
        }
    }

    //纸箱初始化、判断若有数据进行添加
    function cutAddTartontableInit() {
        //隐藏
        $("#addTartonTable_tb2").hide();
        var ddTartonTableTypeColumns=[[
            {field:'name',width:120,align:'center',title:'名称',editor:{type:'textbox',options:{prompt:"请输入外箱名称",'value':"外箱"}}},
            {field:'len',width:40,align:'center',title:'长',editor:{type:'numberbox',options:{precision:4,onChange:addTartonTableOnChange,required:true}}},
            {field:'w',width:40,align:'center',title:'宽',editor:{type:'numberbox',options:{precision:4,onChange:addTartonTableOnChange,required:true}}},
            {field:'h',width:40,align:'center',title:'高',editor:{type:'numberbox',options:{precision:4,onChange:addTartonTableOnChange,required:true}}},
            {field:'price',width:40,align:'center',title:'单价',editor:{type:'numberbox',options:{precision:4,onChange:addTartonTableOnChange,required:true}}},
            {field:'price1',width:60,align:'center',title:'计算结果',editor:{type:'numberbox',options:{precision:4,readonly:true,required:true}}},
            {field:'t',width:60,align:'center',hidden:true,editor:{type:'numberbox'}},
            {field:'randomNum',width:60,align:'center',hidden:true,editor:{type:'textbox'}},
        ]];
        $("#addTartonTable").edatagrid({
            fitColumns:true,
            columns:ddTartonTableTypeColumns,
            toolbar:"#addTartonTable_tb"
        })
        getAddTartonTableData(cartonTableData,1);
    }

    function cutAddTartonTableType2(){
        $("#addTartonTable_tb").hide();
        $("#addTartonTable").edatagrid({
            fitColumns:true,
            columns:[[
                {field:'name',width:120,align:'center',title:'名称',editor:{type:'textbox',options:{prompt:"请输入龙卡(啤)名称",value:'龙卡(啤)'}}},
                {field:'len',width:40,align:'center',title:'长',editor:{type:'numberbox',options:{precision:4,onChange:addTartonTableOnChange2,required:true}}},
                {field:'w',width:40,align:'center',title:'宽',editor:{type:'numberbox',options:{precision:4,onChange:addTartonTableOnChange2,required:true}}},
                {field:'price',width:40,align:'center',title:'单价',editor:{type:'numberbox',options:{precision:4,onChange:addTartonTableOnChange2,required:true}}},
                {field:'h',width:60,align:'center',title:'刀卡数量',editor:{type:'numberbox',options:{precision:4,onChange:addTartonTableOnChange2,required:true}}},
                {field:'price1',width:60,align:'center',title:'计算结果',editor:{type:'numberbox',options:{precision:4,readonly:true,required:true}}},
                {field:'t',width:60,align:'center',hidden:true,editor:{type:'numberbox'}},
                {field:'randomNum',width:60,align:'center',hidden:true,editor:{type:'textbox'}},
            ]],
            toolbar:"#addTartonTable_tb2"
        })
        getAddTartonTableData(cartonTableDataK,2)

    }

    function getAddTartonTableData(data,t) {
        //先从临时变量中获取，若没有从数据库中获取
        if (data.length>0){
            data.forEach(function (value) {
                $("#addTartonTable").edatagrid('addRow',{
                    row:{
                        name:value.name,
                        len:value.len,
                        w:value.w,
                        h:value.h,
                        price:value.price,
                        price1:value.price1,
                        t:value.t
                    }
                })
                $("#addTartonTable").edatagrid('saveRow');
            })
        }
    }

    //龙卡(啤)计算方式
    function  addTartonTableOnChange2(newValue,oldValue){
        var editors = $('#addTartonTable').datagrid('getEditors', getRowIndex(this));
        var len=Number((editors[1].target).numberbox("getValue"))       //长
        var w=Number((editors[2].target).numberbox("getValue"))         //宽
        var price=Number((editors[3].target).numberbox("getValue"))         //单价
        var k=Number((editors[4].target).numberbox("getValue"))     //刀卡数量
        //获取装箱数量
        var quantity=Number($("#costReport_Form_encasementQuantity").textbox("getValue"))
        var price1=editors[5].target            //
        var result=(len/2.54+0.5)*(w/2.54+0.5)*price/quantity*k+0.05; //
        $(price1).numberbox("setValue",result)

    }
    //外箱计算方式
    function  addTartonTableOnChange(newValue,oldValue){

        var editors = $('#addTartonTable').datagrid('getEditors', getRowIndex(this));
        var len=Number((editors[1].target).numberbox("getValue"))   //长
        var w=Number((editors[2].target).numberbox("getValue"))     //宽
        var h=Number((editors[3].target).numberbox("getValue"))     //高
        var price=Number((editors[4].target).numberbox("getValue")) //单价
        var price1=editors[5].target
        //(长+宽+5.08)*(宽+高+2.54)*2*单价/6451.6）
        var result=fomatFloat((len+w+5.08)*(w+h+2.54)*2*price/6451.6,2);
        $(price1).numberbox("setValue",result)

    }

</script>